//
//  String+Ex.swift
//  Browser
//
//  Created by 鹏鹏 on 2021/7/4.
//

import Foundation

extension String {

    /// 获取文件后缀名, 不带.
    var pathExtension: String {
        if let index = self.lastIndex(of: ".") {
            let string = String(self[index...])
            let NIndex = string.index(string.startIndex, offsetBy: 1)
            return String(string[NIndex...])
        } else {
            return ""
        }
    }

    /// 获取文件名, 不带后缀
    var fileNameWithoutPathExtension: String {
        if let index = self.lastIndex(of: ".") {
            let string = String(self[...index])
            return String(string[..<String.Index.init(utf16Offset: string.count - 1, in: string)])
        } else {
            return ""
        }
    }

    /// 简单判断字符串长度
    static func LengthOf(string: String?) -> Int {
        if nil == string {
            return 0
        } else {
            return string!.count
        }
    }

    /// 字符串截取
    func subString(from: Int) -> String {
        let end = self.count
        return String(self[String.Index.init(utf16Offset: from, in: self)..<String.Index.init(utf16Offset: end, in: self)])
    }

    func subString(from: Int, length: Int) -> String {
        let end = from + length
        if self.count < end {
            return self
        }
        return String(self[String.Index.init(utf16Offset: from, in: self)..<String.Index.init(utf16Offset: end, in: self)])
    }

    func subString(from: Int, to: Int) -> String {
        return String(self[String.Index.init(utf16Offset: from, in: self)..<String.Index.init(utf16Offset: to, in: self)])
    }
}
